/*eslint-disable*/
import axios from "axios";
import bus from "@/utils/bus";
import { BACK_API} from  '@/config'
// import router from "@/router";
// import { Toast } from "vant";

const request = axios.create({
  baseURL: BACK_API +  "",
  timeout: 18000 ,// 3 minutes
  withCredentials: true // 允许携带cookie
});

request.interceptors.request.use(
  config => {
    // const token = localStorage.getItem(TOKEN_NAME)
    // config.headers.Authorization = `JWT ${JSON.parse(token)}`
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

request.interceptors.response.use(
  response => {
    // 状态码1002表明未登录
    try {
      if (response.data._status === 1002) {
      // 删除登录的标记量
        localStorage.removeItem('login_time')
        return bus.$emit("show_login_dialog");
      }
     return  response.data;
    } catch (e) {
      Promise.reject(e);
    }
  },
  error => {
    return Promise.reject(error);
  }
);

export default request;
